using System;
using System.IO;
using System.Collections.Generic;
using Sce.PlayStation.Core;
using Sce.PlayStation.Core.Imaging;
using Sce.PlayStation.Core.Environment;
using Sce.PlayStation.HighLevel.UI;

namespace VitaReaderLibPS
{
	public enum UIType
	{
		MAINMENU = 0,
		BOOKS,
		DOWNLOADS,
		SETTINGS,
		READING,
		BOOKMARKS,
		
		
		//
		COUNT,
	};
	
    internal class ReaderUI
    {
		#region data members
		public const int UI_COUNT = (int)UIType.COUNT;
		internal const float LIST_BUTTON_HEIGHT = 40.0f;
		
		//parent
		protected ReaderScene	mScene;
		//UIRoot
		protected Panel			mRoot;
		
		bool					mShowing  = false;
		bool					mHiding = false;
		#endregion
		
		#region basic funcions
		protected bool			Showing	{get {return mShowing;}}
		protected bool			Hiding	{get {return mHiding;}}
		public ReaderScene Scene{get {return mScene;}}
		public Panel RootWidget	{get {return mRoot;}}
		public bool	 Visible	{get {return mRoot.Visible;} set {mRoot.Visible = value;}}
		public void  Show(Effect effect)
		{
			if( effect != null )
			{
				effect.Widget = mRoot;
				effect.EffectStopped += HandleShown;
				mShowing = true;
				effect.Start();
			}
			else
			{
				mRoot.Visible = true;
				this.UpdateLayout( mScene.Layout );
				UISystem.KeyReceiverWidget = this.RootWidget;
				this.OnShown();
			}
			
		}
		public void Hide(Effect effect)
		{
			if( effect != null )
			{
				effect.Widget = mRoot;
				effect.EffectStopped += HandleHidden;
				mHiding = true;
				effect.Start();
			}
			else
			{
				mRoot.Visible = false;
				this.OnHidden();
			}
		}
		public void HandleShown(object sender,EventArgs e)
		{
			mRoot.Visible = true;
			mShowing = false;
			UISystem.KeyReceiverWidget = this.RootWidget;
			this.UpdateLayout( mScene.Layout );
			this.OnShown();
		}
		public void HandleHidden(object sender,EventArgs e)
		{
			mHiding = false;
			mRoot.Visible = false;
			this.OnHidden();
		}
		#endregion
		
		#region uitilites
		//buttons may be invisible due to drage op, when key pressed, make the selected item visible
		//condition: scroll panel has a listpanel as child
		protected void EnsureListItemVisible(ListPanelItem item,ScrollPanel panel)
		{
			Vector2 pos = panel.ConvertLocalToScreen( item.X, item.Y+panel.PanelY );
			
			float topDiff = panel.Y - pos.Y;
			if( topDiff > 0 )
			{
				//
				panel.PanelY += topDiff;
			}
			else
			{
				float bottomDiff = pos.Y + item.Height - (panel.Y + panel.Height);
				if( bottomDiff > 0 )
				{
					panel.PanelY -= bottomDiff;
				}
			}
		}
		#endregion
		
		#region virtuals
		//init ui
		public virtual void Initialize(ReaderScene scene)	{ mScene = scene; }
		//show
		public virtual void OnShown()						{}
		//hide
		public virtual void OnHidden()						{}
		//update layout
		public virtual void UpdateLayout(LayoutOrientation orientation){}
		//upate lang
		public virtual void UpdateLanguage(StreamReader reader) {} 
		//update
		public virtual void Update()		{}
		#endregion
	}
	
	public interface IReadingHandler
	{
		//open file
		void ReadingOpen(String file);
		//close file
		void ReadingClose();
		//continue reading
		bool ReadingContinue(UISwitch effect);
		//open & reading help file
		void ReadingHelp();
		//goto bookpos
		bool ReadingBookPos(int pos);
		//get current reading pos
		bool ReadingTellPos(ref int pos);
	}
}